﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace SilverMindMap.Core.View
{
    public class NullToHiddenConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
                return DependencyProperty.UnsetValue;

            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NotNullToHiddenConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
                return DependencyProperty.UnsetValue;

            return value != null ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class DivideConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.GetType() != typeof(double) || 
                parameter == null || parameter.GetType() != typeof(string) || 
                targetType != typeof(double))
                return DependencyProperty.UnsetValue;

            double parsedParameter = 0.0;
            if (!double.TryParse((string)parameter, out parsedParameter))
                return DependencyProperty.UnsetValue;

            if (parsedParameter == 0.0)
                return DependencyProperty.UnsetValue;

            return (double)value / parsedParameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
